﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;

namespace BusinessLogic
{
    public static class KhuyenMaiTangHang 
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.KhuyenMaiTangHang[] Select()
        {
            Entities.KhuyenMaiTangHang[] list = null;
            try
            {
                string sql = Common.Constants.KhuyenMaiTangHang.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.KhuyenMaiTangHang khuyenmaitanghang = new Entities.KhuyenMaiTangHang();
                    khuyenmaitanghang.KhuyenMaiTangHangID = int.Parse(dr[Common.Constants.KhuyenMaiTangHang.KhuyenMaiTangHangID].ToString());
                    khuyenmaitanghang.MaKhuyenMaiTangHang = dr[Common.Constants.KhuyenMaiTangHang.MaKhuyenMaiTangHang].ToString();
                    khuyenmaitanghang.TenDotKhuyenMai = dr[Common.Constants.KhuyenMaiTangHang.TenDotKhuyenMai].ToString();
                    khuyenmaitanghang.NgayLap = DateTime.Parse(dr[Common.Constants.KhuyenMaiTangHang.NgayLap].ToString());
                    khuyenmaitanghang.NgayBatDau = DateTime.Parse(dr[Common.Constants.KhuyenMaiTangHang.NgayBatDau].ToString());
                    khuyenmaitanghang.NgayKetThuc = DateTime.Parse(dr[Common.Constants.KhuyenMaiTangHang.NgayKetThuc].ToString());
                    arr.Add(khuyenmaitanghang);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.KhuyenMaiTangHang[0];
                }
                else
                {
                    list = new Entities.KhuyenMaiTangHang[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KhuyenMaiTangHang)arr[i];
                    }
                }
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.KhuyenMaiTangHang[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert update
        public static int InsertUpdate(Entities.KhuyenMaiTangHang KhuyenMaiTangHang)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.KhuyenMaiTangHang.InsertUpdate;
                    cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.HanhDong, SqlDbType.NVarChar, 50).Value = KhuyenMaiTangHang.HanhDong;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.KhuyenMaiTangHangID, SqlDbType.Int).Value = KhuyenMaiTangHang.KhuyenMaiTangHangID;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.MaKhuyenMaiTangHang, SqlDbType.VarChar, 50).Value = KhuyenMaiTangHang.MaKhuyenMaiTangHang;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.TenDotKhuyenMai, SqlDbType.NVarChar, 200).Value = KhuyenMaiTangHang.TenDotKhuyenMai;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.NgayBatDau, SqlDbType.DateTime).Value = KhuyenMaiTangHang.NgayBatDau;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.NgayLap, SqlDbType.DateTime).Value = KhuyenMaiTangHang.NgayLap;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.NgayKetThuc, SqlDbType.DateTime).Value = KhuyenMaiTangHang.NgayKetThuc;
                    rt = cmd.ExecuteNonQuery();
                    cn.Close();
                    if (KhuyenMaiTangHang.HanhDong == "Update")
                    {
                        Entities.ChiTietKhuyenMaiTangHang delete = new Entities.ChiTietKhuyenMaiTangHang();
                        delete.MaKhuyenMaiTangHang = KhuyenMaiTangHang.MaKhuyenMaiTangHang;
                        ChiTietKhuyenMaiTangHang.Delete(delete);
                    }
                    if (ChiTietKhuyenMaiTangHang.InsertUpdate(KhuyenMaiTangHang.ChiTietKhuyenMaiTangHang) == 0)
                    {
                        ts.Dispose();
                        return 0;
                    }
                    if (KhuyenMaiTangHang.HanhDong == "Insert")
                    {
                        LuuVietHeThong.LuuViet(
                            "Thêm hóa đơn",
                            KhuyenMaiTangHang.TaiKhoan.MaNhanVien,
                            "Thêm hóa đơn mới " + KhuyenMaiTangHang.MaKhuyenMaiTangHang,
                            KhuyenMaiTangHang.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToString("dd/MM/YYYY"));
                    }
                    else
                    {
                        LuuVietHeThong.LuuViet(
                            "Sửa hóa đơn",
                            KhuyenMaiTangHang.TaiKhoan.MaNhanVien,
                            "Sửa hóa đơn " + KhuyenMaiTangHang.MaKhuyenMaiTangHang,
                            KhuyenMaiTangHang.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToString("dd/MM/YYYY"));
                    }
                    ts.Complete();
                    cn.Close();
                    return rt;
                }
            }
            catch
            {
                ts.Dispose();
                return 0;
            }

        }
        #endregion

        #region delete
        public static int Delete(Entities.KhuyenMaiTangHang KhuyenMaiTangHang)
        {
           TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.KhuyenMaiTangHang.Delete;
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.KhuyenMaiTangHang.MaKhuyenMaiTangHang, SqlDbType.VarChar, 50).Value = KhuyenMaiTangHang.MaKhuyenMaiTangHang;
                    rt = cmd.ExecuteNonQuery();
                    LuuVietHeThong.LuuViet(
                        "Xóa hóa đơn",
                        KhuyenMaiTangHang.TaiKhoan.MaNhanVien,
                        "Xóa hóa đơn " + KhuyenMaiTangHang.MaKhuyenMaiTangHang,
                        KhuyenMaiTangHang.TaiKhoan.TenDangNhap,
                        DateTime.Now.ToString("dd/MM/YYYY"));
                    ts.Complete();
                    cn.Close();
                    return rt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                return 0;
            }

        }
        #endregion
    }
}
#endregion